package com.superiron.scaffold.core.service.mail;

import cn.hutool.extra.mail.MailAccount;
import cn.hutool.extra.mail.MailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.InputStream;
import java.util.Collection;
import java.util.Map;

/**
 * @author yzg
 * @date 2022年7月6日14:00:22
 * 客户端类
 */
@Component
public class MailService {
	
	@Autowired
	private MailProperties mailConfig;
	
	MailAccount getAccount() {
		//初始化连接
		MailAccount account = new MailAccount();
		account.setHost(mailConfig.getHost());
		account.setPort(mailConfig.getPort());
		if (mailConfig.getAuth() != null) {
			account.setAuth(mailConfig.getAuth() );
		}
		account.setFrom(mailConfig.getFrom());
		account.setUser(mailConfig.getFrom());
		account.setPass(mailConfig.getPass());
		account.setStarttlsEnable(mailConfig.isStarttlsEnable());
		account.setSslEnable(mailConfig.getSslEnable());
		account.setSocketFactoryClass(mailConfig.getSocketFactoryClass());
		account.setSocketFactoryFallback(mailConfig.isSocketFactoryFallback());
		account.setSocketFactoryPort(mailConfig.getSocketFactoryPort());
		return account;
	}
	/**
	 * 发送邮件
	 *
	 * @param tos     收件人
	 * @param subject 标题
	 * @param content 内容
	 * @param isHtml  是否html格式
	 * @param files   附件
	 *
	 * @return
	 */
	public String send(Collection<String> tos, String subject, String content, boolean isHtml, File... files) {
		if (mailConfig.getIsEnable()) {
			MailAccount account = getAccount();
			return MailUtil.send(account, tos, subject, content, isHtml, files);
		}
		return null;
	}
	
	public String send(Collection<String> tos, String subject, String content, Map<String, InputStream> imageMap, boolean isHtml, File... files) {
		if (mailConfig.getIsEnable()) {
			MailAccount account = getAccount();
			return MailUtil.send(account, tos, subject, content, imageMap, isHtml, files);
		}
		return null;
	}
	
	public String send(String to, String subject, String content, boolean isHtml, File... files) {
		if (mailConfig.getIsEnable()) {
			MailAccount account = getAccount();
			return MailUtil.send(account, to, subject, content, isHtml, files);
		}
		return null;
	}
	
}




